//异步请求，获取获取页面json
var alldata;

$.ajaxSetup({
  async: false
  });

function getMessage(user,page) {
    var json={"user":user,"page":page}
    $.get("http://139.199.200.112/dtpr/index.php?exam-master-basic-publicmessage",json,function (data) {
        data=JSON.parse(data);
        alldata=data;
    })
}

//根据页面url来判断应该跳转到哪个页面
var page=getQueryString("page");
if (!page){
    page=1;
    
}
var user=getQueryString("user");

if(user!=1&&user!=0){
    user=0;
}
getMessage(user,page);
var data=alldata.data;
//选择班消息类型
function selectType(type) {
    if(type===0){
        window.location="http://139.199.200.112/dtpr/index.php?exam-master-basic-pmessagecontrol&user=0&page="+1;
    }else {
        window.location="http://139.199.200.112/dtpr/index.php?exam-master-basic-pmessagecontrol&user=1&page="+1;
    }
}

//生成表
function table() {
    var nodes=data.data;
    for (var i=0;i<nodes.length;i++){
        document.writeln("<tr>");
        document.writeln("<td>"+nodes[i].id+"</td>");
        document.writeln("<td>"+nodes[i].title+"</td>");
        document.writeln("<td>"+nodes[i].content+"</td>");
        var myDate=new Date(nodes[i].sendtime);
        document.writeln("<td>"+myDate+"</td>");
        document.writeln("</tr>");
    }
}

//生成分页栏
function messagePage() {
    //生成最大最小页码
    var maxPage=parseInt(page)+3;
    var minPage=parseInt(page)-3;
    if(minPage<1){
        maxPage+=1-minPage;
        minPage=1;
    }

    if(maxPage>Math.ceil(data.number/10)){
        minPage-=maxPage-Math.ceil(data.number/10);
        maxPage=Math.ceil(data.number/10);
        if(minPage<1){
            minPage=1;
        }
    }

    document.writeln("<nav aria-label='Page navigation'>\n" +
        "                    <ul class='pagination'>\n" +
        "                        <li>\n" +
        "                            <a href='"+getURL(1)+"' aria-label='Previous'>\n" +
        "                                <span aria-hidden='true'>&laquo;</span>\n" +
        "                            </a>\n" +
        "                        </li>");

    for(var i=minPage;i<=maxPage;i++){
        if(i==page){
            document.writeln("<li class='active'><a href='"+getURL(i)+"'>"+i+"</a></li>");
        }else {
            document.writeln("<li><a href='"+getURL(i)+"'>"+i+"</a></li>");
        }
    }

    document.writeln("<li>\n" +
        "                            <a href='"+getURL(Math.ceil(data.number/10))+"' aria-label='Next'>\n" +
        "                                <span aria-hidden='true'>&raquo;</span>\n" +
        "                            </a>\n" +
        "                        </li>\n" +
        "                    </ul>\n" +
        "                </nav>");
}
//生成跳转url
function getURL(page) {
    return "http://139.199.200.112/dtpr/index.php?exam-master-basic-pmessagecontrol&user="+user+"&page="+page;
}


//根据内容生成表格
function initButton() {
    if(user==0){
        $("#teacher").attr("class","btn btn-primary");
        $("#student").attr("class","btn btn-default");
    }else {
        $("#student").attr("class","btn btn-primary");
        $("#teacher").attr("class","btn btn-default");
    }
};

